package indi.zhifa.learn.jenkins.helloworld2.web.service.algorithm.datastructure;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;

import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.Queue;

public class PdfOutlineTreeRoot extends PdfOutlineTreeNode{

    public void init(PDDocument pDoc) throws IOException {
        title = "root";
        PDDocumentCatalog pdDocumentCatalog = pDoc.getDocumentCatalog();
        PDDocumentOutline documentOutline = pdDocumentCatalog.getDocumentOutline();
        // 宽搜找出所有的目录节点
        Iterator<PDOutlineItem> outlineIt = documentOutline.children().iterator();
        // 宽搜的队列
        Queue<PdfOutlineTreeNode> queue = new ArrayDeque<>();
        while (outlineIt.hasNext()){
            PDOutlineItem pdOutlineItem = outlineIt.next();
            PdfOutlineTreeNode childNode = setChild(this,pdOutlineItem);
            queue.add(childNode);
        }
        // 宽搜设置子节点
        PdfOutlineTreeNode preNode = this;
        while(!queue.isEmpty()){
            PdfOutlineTreeNode node = queue.poll();
            Iterator<PDOutlineItem> outlineChildIt = node.beginBookMark.children().iterator();
            while (outlineChildIt.hasNext()){
                PDOutlineItem pdOutlineItem = outlineChildIt.next();
                PdfOutlineTreeNode childNode = setChild(node,pdOutlineItem);
                queue.add(childNode);
            }
            // 为之前的节点设置后继节点
            if(null == preNode.endBookMark){
                preNode.endBookMark = node.beginBookMark;
            }
            preNode = node;
        }
        // 遍历所有节点
        queue.clear();
        queue.add(this);
        while (!queue.isEmpty()){
            PdfOutlineTreeNode node = queue.poll();
            node.prepare(pDoc);
            for(PdfOutlineTreeNode pdfOutlineTreeNode : node.children.values()){
                queue.add(pdfOutlineTreeNode);
            }
        }
    }

    private PdfOutlineTreeNode setChild(PdfOutlineTreeNode pRoot, PDOutlineItem pChildOutline){
        PdfOutlineTreeNode pdfOutlineTreeNode = new PdfOutlineTreeNode();
        PDOutlineItem pdOutlineItem = pChildOutline;
        pdfOutlineTreeNode.setTitle(pdOutlineItem.getTitle().trim());
        pdfOutlineTreeNode.setBeginBookMark(pdOutlineItem);
        pRoot.children.put(pdfOutlineTreeNode.getTitle(),pdfOutlineTreeNode);
        return pdfOutlineTreeNode;
    }

    public PdfOutlineTreeNode findNode(String[] pOutlines) throws Exception {
        PdfOutlineTreeNode pdfOutlineTreeNode = this;
        for(String outline : pOutlines){
            pdfOutlineTreeNode = pdfOutlineTreeNode.getChild(outline);
        }
        return pdfOutlineTreeNode;
    }
}
